import pymysql
from faker import Faker

class table():
    def data(self):
        # 创建 Faker 实例，指定语言为中文
        fake = Faker('zh_CN')

        # 连接到 MySQL 数据库
        conn = pymysql.connect(
            host='127.0.0.1',
            user='root',
            password='123456Aa',
            database='help',
            charset='utf8mb4'
        )

        try:
            with conn.cursor() as cursor:
                # 开始插入数据
                for _ in range(1000000):
                    name = fake.name()
                    email = fake.email()
                    address = fake.address().replace('\n', ' ')

                    # 执行插入语句
                    sql = "INSERT INTO aa (name, email, address) VALUES (%s, %s, %s)"
                    cursor.execute(sql, (name, email, address))

                # 提交事务
                conn.commit()
                print("100 万条数据插入成功！")
        except Exception as e:
            print(f"插入数据时出现错误: {e}")
            # 回滚事务
            conn.rollback()
        finally:
            # 关闭数据库连接
            conn.close()
